package com.zgzg.shop.pay.base.utils;

import cn.hutool.core.codec.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* Base64工具类
* @author qy
* @since 2023/1/11 16:39
*
**/
public class Base64Util {

    private static final Logger log = LoggerFactory.getLogger(Base64Util.class);

    /**
    * 根据url获取资源的base64字符串
    * @author qy
    * @since 2023/1/11 16:53
    * @param sourceUrl: 资源url
    * @return base64字符串
    **/
    public static String file(String sourceUrl) {
        String encode = null;
        try {
            URL url = new URL(sourceUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            InputStream inputStream = conn.getInputStream();
            encode = Base64.encode(inputStream);
        } catch (Exception e) {
            log.error("获取资源base64加密字符串异常", e);
        }
        return encode;
    }


//    public static void main(String[] args) {
//        System.out.println(Base64Util.file("https://imlf.8531.cn/webpic/W0202301/W020230111/W020230111378333016351.jpg"));
//    }
}